package cn.zero.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import cn.zero.authorize.Authorize;
import cn.zero.service.UserService;
import cn.zero.service.WalletService;

@Controller
@RequestMapping
public class IndexController extends BaseController {

	String prefix = "index/%s";

	@Autowired
	private UserService userService;
	
	@Autowired
	private WalletService walletService;
	
	@GetMapping
	public String index(Map<String, Object> map) {
		Long userId = Authorize.getLoginInfo().getUserId();
		map.put("userAccount", walletService.getByUserId(userId));
		map.put("invitedCount", userService.countInvited(userId));
		map.put("userId", userId);
		return formatPage(prefix, "index");
	}
	
	@GetMapping("/login")
	public String login() {
		return formatPage(prefix, "login");
	}

	@PostMapping("/login")
	public String login(@RequestParam("username") String username, @RequestParam("password") String password) {
		return "redirect:/";
	}
	
	@GetMapping("/signUp")
	public String signUp(@RequestParam(value = "i", required= false) Long invitorId, Map<String, Object> map) {
		map.put("invitorId", invitorId == null ? 0 : invitorId);
		return formatPage(prefix, "signUp");
	}

}
